package com.example.order.activity;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.order.R;
import com.example.order.utils.Utils;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Main4Activity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        final TextView text=findViewById(R.id.text_pay);
        Button button=findViewById(R.id.button_pay1);
        //orderList/sum 查看改订单的总消费
        final String order_id = getIntent().getStringExtra("order_id");
        HashMap<String,String> map=new HashMap<>();
        map.put("order_id",order_id);
        Utils.sendPostHttpRequest("orderList/sum",
                map,
                new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //查询总消费
                        final String string = response.body().string();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Utils.showToast(Main4Activity.this,string,false);
                                text.setText(string);
                            }
                        });
                    }
                });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
                HashMap<String,String> map=new HashMap<>();
                map.put("order_id",order_id);
                Utils.sendPostHttpRequest("order/end",
                        map,
                        new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                                Log.e("Main4Activity", Arrays.toString(e.getStackTrace()));
                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                final String string = response.body().string();
                                Log.i("Main4Activity", string);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        text.setText(string);
                                        Utils.showToast(Main4Activity.this,string,true);

                                    }
                                });

                            }
                        });
            }
        });



    }
}
